package com.je.app.persistence;

import java.io.FileInputStream;
import java.util.Properties;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

import com.je.app.control.ControlCaja;
import com.je.app.control.ControlStock;
import com.je.app.control.ControlTurno;
import com.je.app.view.MainFrame;
import com.je.app.view.popUp.ValidarUsuario;

public class HibernateUtil {
 
	private static SessionFactory sessionFactory ;
	private static Session session ;

    private static SessionFactory buildSessionFactory() {
        try {
            // Create the SessionFactory from hibernate.cfg.xml
        	java.util.Properties properties = new Properties();
        	properties.load(new FileInputStream("src/main/resources/hibernate.properties"));
        	Configuration cfg = new AnnotationConfiguration().configure();
        	cfg.addProperties(properties); 
        	return cfg.buildSessionFactory();
 
        }
        catch (Throwable ex) {
            // Make sure you log the exception, as it might be swallowed
            System.err.println("Initial SessionFactory creation failed." + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
    	if(sessionFactory==null)
    		sessionFactory = buildSessionFactory();
    	return sessionFactory;
    }
    
    public static Session getSession(){
    	if(sessionFactory==null)
    		sessionFactory = buildSessionFactory();
    	if(session==null)
    		session = sessionFactory.openSession();
    	return session;
    }

}
